#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>

bool isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}

int main() {
int n;
std::cin >> n;

if (n < 1000 || n > 9999) {
std::cout << "0" << std::endl;
return 0;
}

std::string num = std::to_string(n);
std::sort(num.begin(), num.end());
int maxPrime = 0;

do {
for (int i = 1; i < num.size(); i++) {
std::string subNum = num.substr(0, i);
int numVal = std::stoi(subNum);
if (isPrime(numVal) && numVal > maxPrime) {
maxPrime = numVal;
}
}
} while (std::next_permutation(num.begin(), num.end()));

std::cout << maxPrime << std::endl;

return 0;
}
